package com.example.nna.main;

import java.util.ArrayList;
import java.util.Iterator;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.nna.contain.containPage2;
import com.example.nna.listview.listViewCustom2;
import com.example.nna.listview.listViewCustom2.OnActionItemClickListener;
import com.example.nna.voz.taskLoadImage;

public class Page2 extends Page{
	private ArrayList<containPage2> ListContains;
	private listViewCustom2 adapter;
	private ImageButton mMenuGo;
	private TextView mTextNumPage;
	private RelativeLayout mMenuGoLayout;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.page2);
		super.onCreate(savedInstanceState);
		
		mList = (ListView)findViewById(R.id.mainlist);
		ListContains = new ArrayList<containPage2>();
		adapter = new listViewCustom2(mActivity,ListContains);
	    adapter.setSize(mTextSize);
	    mList.setAdapter(adapter);
	    log(URL+getIntent().getStringExtra("URL"));
	    mMenuTitle.setText(getIntent().getStringExtra("TITLE"));
	    mParser.setUrl(URL+getIntent().getStringExtra("URL"));
	    mTask.execute(TASK_GETDOC);
	    
	    adapter.setOnActionItemClickListener(new OnActionItemClickListener() {
			
			@Override
			public void onItemClick(int pos, int actionId) {
				// TODO Auto-generated method stub
				Intent i = new Intent(mContext, Page3.class);
				i.putExtra("URL", ListContains.get(pos).UrlThread());
				i.putExtra("TITLE", ListContains.get(pos).Thread());
				startActivity(i);
				overridePendingTransition(R.anim.rail, 
										  R.anim.rail);
			}
		});
	    
	}
	@Override
	protected void updateSetFont(float value) {
		// TODO Auto-generated method stub
		super.updateSetFont(value);
		adapter.setSize(value);
		adapter.notifyDataSetChanged();
	}

	@Override
	void Parser() {
		// TODO Auto-generated method stub
		super.Parser();
		ListContains.clear();
    	Element eleTag1,eleTag1Image ,eleTag1Time ,eleTag1Replies,eleTag1Views ;
		Element eleLogin = doc.select("a[href=private.php]").first();
		mTextNumPage.setText(parsePage(GO_INDEXPAGE));
		if(eleLogin != null){
			String temp = doc.select("td[class=alt2]").get(0).select("a[href*=mem]").attr("href");
			temp = temp.split("=")[1];
			mUser.setUserId(temp);
		}
		Bitmap bitmap = null;
		String urlImage,text1 = null,text2=null,text3=null,text4=null,text1_url1=null,text2_url2=null;
		Element mainElement = doc.select("tbody:has(form[id])").first();
		if(mainElement == null) return;
		Elements eleTag1s = mainElement.select("tr:has(td[id*=td_threads])");
		Elements eleTag2s = mainElement.select("tr > td[class=alt1Active]");
		Iterator<Element> iteTag1 = eleTag1s.iterator();
		Iterator<Element> iteTag2 = eleTag2s.iterator();
		if(iteTag2.hasNext())	ListContains.add(new containPage2("Sub-Forum",null,null,null,null,null,null));
		while(iteTag2.hasNext()){
			eleTag1 = iteTag2.next();
			eleTag1Image = eleTag1.select("td > img[id]").first();
			eleTag1Time = eleTag1.nextElementSibling();
			eleTag1Replies = eleTag1Time.nextElementSibling();
			eleTag1Views = eleTag1Replies.nextElementSibling();
			if(eleTag1Image != null){ 
				urlImage= eleTag1Image.select("img").attr("src");
				bitmap = loadBitmapAssert(urlImage); 
			}
			if(eleTag1 != null) text1 = eleTag1.text();
			if(eleTag1.select("div > a").first() != null) 
				text1_url1 = eleTag1.select("div > a").first().attr("href");
			if(eleTag1Time.select("span > a").first() != null){
				text2 = eleTag1Time.select("span > a").first().text();
				text2_url2 = eleTag1Time.select("span > a").first().attr("href");
			}
			if(eleTag1Replies != null)		text3 = eleTag1Replies.text();
			if(eleTag1Views != null) text4 = eleTag1Views.text();
			ListContains.add(new containPage2(text1,text2, text3,text4,	bitmap,	text1_url1, text2_url2));
			adapter.notifyDataSetChanged();
		}
		ListContains.add(new containPage2("Thread",null,null,null,null,null,null));
		while(iteTag1.hasNext()){
			eleTag1 = iteTag1.next();
			//log(eleTag1.text());
			if(eleTag1.select("a[id*=thread_title").first() != null){
				text1 = eleTag1.select("a[id*=thread_title").first().text();
				text1_url1 = eleTag1.select("a[id*=thread_title").first().attr("href");
			}
			if(eleTag1.select("td[class=alt2]:has(span)").first() != null){
				text2 = eleTag1.select("td[class=alt2]:has(span)").first() .text();
			}
			if(eleTag1.select("td[align=center]").first() != null){
				text3 = eleTag1.select("td[align=center]").get(0).text();
				text4 = eleTag1.select("td[align=center]").get(1).text();
			}
			if(eleTag1.select("img[id*=thread_statusicon]").first() != null){
				bitmap = loadBitmapAssert(eleTag1.select("img[id*=thread_statusicon]").first().attr("src")); 
			}
			if(eleTag1.select("a[id*=thread_gotonew]").first() != null){
				text2_url2 = eleTag1.select("a[id*=thread_gotonew]").first().attr("href");
			}
			ListContains.add(new containPage2(text1,text2,text3,text4,bitmap,text1_url1,text2_url2));
			if(eleTag1.hasClass("vozsticky")){
				ListContains.get(ListContains.size()-1).setSticky(true);
				//toast("sticky"+ListContains.get(ListContains.size()-1).Thread());
			}else{
				ListContains.get(ListContains.size()-1).setSticky(false);
			}
			adapter.notifyDataSetChanged();
		}
		
	}
	@Override
	void initUI() {
		// TODO Auto-generated method stub
		super.initUI();
		mMenuGo = (ImageButton)findViewById(R.id.menu_go);
		mMenuGoLayout = (RelativeLayout)findViewById(R.id.panel_go);
		mTextNumPage = (TextView)findViewById(R.id.Go_NumPage);
		mMenuGo.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				toast("click");
				if(mMenuGoLayout.getVisibility() == View.GONE)
					mMenuGoLayout.setVisibility(View.VISIBLE);
				else
					mMenuGoLayout.setVisibility(View.GONE);
			}
		});
		ImageButton mGo = (ImageButton)findViewById(R.id.Page2Page);
		ImageButton mNext = (ImageButton)findViewById(R.id.Page2Next);
		ImageButton mFirst = (ImageButton)findViewById(R.id.Page2First);
		ImageButton mPrev = (ImageButton)findViewById(R.id.Page2Previous);
		ImageButton mLast = (ImageButton)findViewById(R.id.Page2Last);
		
		mGo.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//toast(parsePage(GO_REFRESH));
				mParser.setUrl(parsePage(GO_REFRESH));
				mTask.cancel(true);
				mTask = new TaskGetHtml();
				mTask.execute(TASK_GETDOC);
			}
		});
		mNext.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//toast(parsePage(GO_NEXT));
				mParser.setUrl(parsePage(GO_NEXT));
				mTask.cancel(true);
				mTask = new TaskGetHtml();
				mTask.execute(TASK_GETDOC);
				
			}
		});
		mFirst.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//toast(parsePage(GO_FIRST));
				mParser.setUrl(parsePage(GO_FIRST));
				mTask.cancel(true);
				mTask = new TaskGetHtml();
				mTask.execute(TASK_GETDOC);
			}
		});
		mPrev.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//toast(parsePage(GO_PREVIOUS));
				mParser.setUrl(parsePage(GO_PREVIOUS));
				mTask.cancel(true);
				mTask = new TaskGetHtml();
				mTask.execute(TASK_GETDOC);
			}
		});
		mLast.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//toast(parsePage(GO_LAST));
				mParser.setUrl(parsePage(GO_LAST));
				mTask.cancel(true);
				mTask = new TaskGetHtml();
				mTask.execute(TASK_GETDOC);
			}
		});
		
	}
}
